﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_VideoGalery : System.Web.UI.Page
{
    protected LocalFunctions FUNC;

    protected void Page_Load(object sender, EventArgs e)
    {
        FUNC = ((Admin_AdminMasterPage)Master).FUNC;
        //get all galeries from db
        if(!IsPostBack)
            GetData();
    }

    private void GetData()
    {
        Dal.arParams.Clear();
        Dal.arParams.Add(1); // video
        dlGallery.DataSource = Dal.ExecuteDBsp("Admin_SelectAllGalleries",Dal.arParams);
        dlGallery.DataBind();
    }

    protected void imgGallery_Click(object sender, ImageClickEventArgs e)
    {
        int iGalleryID = Convert.ToInt32(((ImageButton)sender).CommandArgument);
        Response.Redirect("video.aspx?ID=" + iGalleryID);
    }

    protected void btnAddGalery_Click(object sender, EventArgs e)
    {
        int iGaleryID;
        int.TryParse(HfID.Value,out iGaleryID);
        if (txtTitleRU.Text.Trim() != string.Empty && txtTitleHEB.Text.Trim() != string.Empty)
        {
            Dal.arParams.Clear();
            Dal.arParams.Add(iGaleryID);
            Dal.arParams.Add(txtTitleRU.Text);
            Dal.arParams.Add(txtTitleHEB.Text);
            Dal.arParams.Add(Helper.ConvertStringToSql(txtDescRU.Text));
            Dal.arParams.Add(Helper.ConvertStringToSql(txtDescHEB.Text));
            Dal.arParams.Add(1); // video galery
            Dal.ExecuteDBsp("Admin_AddGallery", Dal.arParams, true);
            GetData();

            HfID.Value = "0";
            txtTitleRU.Text = string.Empty;
            txtTitleHEB.Text = string.Empty;
            txtDescRU.Text = string.Empty;
            txtDescHEB.Text = string.Empty;
        }
    }

    protected void imgDelete_Click(object sender, ImageClickEventArgs e)
    {
        int iGalID;
        Int32.TryParse(((ImageButton)sender).CommandArgument.ToString(), out iGalID);
        
        Dal.arParams.Clear();
        Dal.arParams.Add(iGalID);
        Dal.ExecuteDBsp("Admin_DeleteGallery", Dal.arParams);
        GetData();
    }

    protected void imgEdit_Click(object sender, ImageClickEventArgs e)
    {
        int iGalID;
        Int32.TryParse(((ImageButton)sender).CommandArgument.ToString(), out iGalID);

        Dal.arParams.Clear();
        Dal.arParams.Add(iGalID);
        DataTable DT=Dal.ExecuteDBsp("Admin_GetGallery", Dal.arParams);
        if (DT != null)
        {
            HfID.Value = DT.Rows[0]["ID"].ToString();
            txtTitleRU.Text=DT.Rows[0]["TitleRU"].ToString();
            txtTitleHEB.Text = DT.Rows[0]["TitleHEB"].ToString();
            txtDescRU.Text = Helper.ConvertStringFromSql(DT.Rows[0]["DescriptionRU"].ToString());
            txtDescHEB.Text = Helper.ConvertStringFromSql(DT.Rows[0]["DescriptionHEB"].ToString());
        }
        GetData();

    }
    protected void dlGallery_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        AjaxControlToolkit.ConfirmButtonExtender cb = new AjaxControlToolkit.ConfirmButtonExtender();
        try
        {
            cb.TargetControlID = e.Item.FindControl("imgDelete").ClientID.Replace("_", "$");
            cb.OnClientCancel = "onCancel";
            cb.ConfirmText = "Вы уверены , что хотите удалить запись ?";
            e.Item.Controls.Add(cb);
        }
        catch { }
    }
}
